const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    AvatarText:'选择头像',
    avatarUrl:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    nickName: '',
    getUserInfoData: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 授权登录
   */
  getUserInfo(e) {
    let _this = this;
    console.log(_this.data);
    if(_this.data.nickName == '' || _this.data.AvatarText != ''){
      wx.showToast({
        title: '头像或昵称未设置',
        icon: 'none'
      })
      return;
    }
    this.setData({
      getUserInfoData: e
    },function(){
      _this.doUpLoadAvatar(function(){
        App.getUserInfo(_this.data.getUserInfoData, () => {
          // 跳转回原页面
          _this.onNavigateBack();
        });
      })
    });
  },
  doUpLoadAvatar:function(callback){
    var that = this;
    wx.uploadFile({
      url: App.api_root + 'UploadFile',
      filePath: this.data.avatarUrl,
      name: 'uploadfile_ant',
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function (res) {
        var avatar = JSON.parse(JSON.parse(res.data).RetJson)[0];
        that.data.getUserInfoData.detail.nickname = that.data.nickName
        that.data.getUserInfoData.detail.avatar = App.ServerFileHost + avatar
        that.setData({
          getUserInfoData: that.data.getUserInfoData
        },function(){
          if(callback)
            callback();
        })
      },
      fail: function (res) {
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function (res) { }
        })
      }
    });
    
    
  },
  /**
   * 暂不登录
   */
  onNotLogin() {
    let _this = this;
    // 跳转回原页面
    _this.onNavigateBack();
  },

  /**
   * 授权成功 跳转回原页面
   */
  onNavigateBack() {
    wx.navigateBack();
  },
  getUserAvatar(s, e){
    console.log(s)
    this.setData({
      avatarUrl: s.detail.avatarUrl,
      AvatarText: ''
    })
  },
  doInputNickName(s, e){
    this.setData({
      nickName: s.detail.value
    })
  },
  doNickNameReview(s, e){
    console.log(s, e)
  }
})